/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Accelerometer;

import Communication.Communicator;
import Wasa.Acceleration;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gianmariospacagna
 */
public class AccStreamMeasurement extends Thread {

    private Communicator myCommunicator;
    private int rate;
    private ArrayList<Acceleration> accMeasures;

    public AccStreamMeasurement(Communicator c, int rate) {
        this.myCommunicator = c;
        this.rate = rate;
        this.accMeasures=new ArrayList<Acceleration>();

    }

    @Override
    public void run() {
        myCommunicator.startAccStream(this, rate);
        int i=0;
        
        while(true){
            try {
            sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(AccStreamMeasurement.class.getName()).log(Level.SEVERE, null, ex);
        }
            checkNewData();
        }
            
    }

    public void newDataAvailable(ArrayList<Acceleration> data) {
        //System.out.println("AccStreamMeasurement: Acceleration Data received");
        for (Acceleration a : data) {
            System.out.println("ASM: "+a.toString());
        }
    }

    private void checkNewData() {
        accMeasures.addAll(myCommunicator.getNewData());
        //myCommunicator.getNewData();
        for (Acceleration a : accMeasures) {
            System.out.println("ASM: "+a.toString());
        }
    }
}
